package com.zhonghan.common.wechat.api;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

import org.apache.log4j.Logger;

/**
 * 微信服务器认证配置信息
 */
public class AuthApi {
	
	private static Logger logger = Logger.getLogger(AuthApi.class);

	/**
	 * APPID
	 */
	public static String APPID = "wxe61425392b419148";
	
	/**
	 * TOKEN
	 */
	public static String TOKEN = "wechat";
	
	/**
	 * APPSECRET
	 */
	public static String APPSECRET = "d93b6886967b45cd989ae0308d501706";
	
	
	/**
	 * 验证服务器
	 * 将传入的token,timestampm,nonce按照字典顺序进行排序
	 * @param token
	 * @param timestamp
	 * @param nonce
	 * @return
	 */
	public static String sort(String token, String timestamp, String nonce) {
		
		String[] strArray = { token, timestamp, nonce };

		Arrays.sort(strArray);

		StringBuilder sb = new StringBuilder();

		for (String str : strArray) {
			sb.append(str);
		}

		return sb.toString();
	}
	
	/**
	 * 将传入的token,timestampm,nonce按照字典顺序进行排序后进行加密
	 * 
	 * @param str
	 * @return
	 */
	public static String encryt(String str) {

		try {
			MessageDigest digest = MessageDigest.getInstance("SHA-1");
			digest.update(str.getBytes());
			byte[] messageDigest = digest.digest();

			// 创建十六进制字符串
			StringBuffer hexString = new StringBuffer();

			// 字节数组转换为十六进制数
			for (int i = 0; i < messageDigest.length; i++) {
				String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);

				if (shaHex.length() < 2) {
					hexString.append(0);
				}
				hexString.append(shaHex);
			}
			return hexString.toString();

		} catch (NoSuchAlgorithmException e) {
			logger.debug("加密方式出错");
			e.printStackTrace();
		}

		return "";
	}
	
	
	
	
	
	
	
}
